/*
* @Author: fangshuqin
* @Date:   2018-05-07 14:24:58
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-26 18:52:00
*/
{
    let EVENT_CLICK = Laya.Event.CLICK;

    
    class Roledetail extends ui.Role.DetailUI {
        constructor () {
            super();
            this.init();
        }

        init () {
            this.initEvent();
            this.initConfig();
            this.registerActions();
        }

        // 初始化配置
        initConfig() {
            this.config = {
                MAX_WIDTH: 298,
                MIN_WIDTH: 10

            }
            this.showIdx =null;
            this.heroDetail = null;
            this.upgrade = false;
            this.goBackCallBack = null;
            this.mapId =null;
            this.type = null;

        }

        showOrHide(idx , mapId, type){
            // 详情，带升级按钮
            this.showIdx = idx;
            this.mapId = mapId;
            this.type = type;
            console.log("this.mapId this.type",this.mapId, this.type);
            if(idx){
                this.role_detail_box.visible = true;
                this.bg_detail_up.visible = true;


                this.heat_box.y = 275;
                this.damag_box.y= 339;
                this.effect_box.y =403;
                this.dom_level.y = 242;

                this.heatBox.visible = true;
                this.damageBox.visible = true;
                this.effectBox.visible = true;

                this.bg_detail.visible = false;
                this.btn_confirm.visible = false;
                this.progressBox.visible = false;
            }else{

                this.bg_detail.visible = true;
                this.btn_confirm.visible = true;
                this.progressBox.visible = true;
                this.heat_box.y = 289;
                this.damag_box.y= 357;
                this.effect_box.y =426;
                this.dom_level.y = 251;

                this.heatBox.visible = false;
                this.damageBox.visible = false;
                this.effectBox.visible = false;

                this.role_detail_box.visible = false;
                this.bg_detail_up.visible = false;
            }
        }
   
        registerActions() {
            this.ACTIONS = {
                 ["heroLevel"]  : this.getSingleHeroLevel, //单个英雄
                 [GAME_CMDS.UPBYITEM]            : this.getSingleHeroDetail, //升级
                 [GAME_CMDS.SKILLDETAIL]         : this.heroSkillDetail   //技能描述
            };
            Sail.io.register(this.ACTIONS, this);
        }

        getSingleHeroLevel(data){
     
            let heroDetail = this.heroDetail = data.level[data.curLevel-1];
            let heroNextDetail = data.level[data.curLevel];
            console.log("getSingleHeroDetail",this.heroDetail);

            // 血量
            let currenthp = heroDetail.hp;
            let _progress_hp = currenthp/ data.max.hpMax ;
            let per_hp = Math.floor(_progress_hp * 100);
            let w_hp = per_hp / 100 * this.config.MAX_WIDTH;
            w_hp = w_hp < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : w_hp;

            // 普攻
            let currentdamage = heroDetail.damage;
            let _progress_damage = currentdamage/ data.max.damageMax ;
            let per_damage = Math.floor(_progress_damage * 100);
            let w_damage = per_damage / 100 * this.config.MAX_WIDTH;
            w_damage = w_damage < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : w_damage;

           // 技能
            let currenteffect = heroDetail.skill;
            let _progress_effect = currenteffect/ data.max.skillMax ;
            let per_effect = Math.floor(_progress_effect * 100);
            let w_effect = per_effect / 100 * this.config.MAX_WIDTH;
            w_effect = w_effect < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : w_effect;

            this.progress_heat.width = w_hp;
            this.progress_damage.width = w_damage;
            this.progress_effect.width = w_effect;

            this.dom_level.text = heroDetail.level + '级';
            this.num_heat.text = heroDetail.hp;
            this.num_damage.text = heroDetail.damage;
            this.num_effect.text = heroDetail.skill;

            if(heroDetail.level<9){
                this.num_addheat.text = '+'  + (heroNextDetail.hp     - heroDetail.hp);
                this.num_adddamage.text = '+'+ (heroNextDetail.damage - heroDetail.damage);
                this.num_addeffect.text ='+' + (heroNextDetail.skill  - heroDetail.skill);

                this.num_addheat.visible = true;
                this.num_adddamage.visible = true;
                this.num_addeffect.visible = true;

            }else{

                this.num_addheat.visible = false;
                this.num_adddamage.visible = false;
                this.num_addeffect.visible = false;
            }
            
            // 角色详情需要的数据
            if(this.showIdx ==1){
                this.dom_coin.text = heroDetail.coin;
                this.dom_debris.text =  data.chips+ '/' + heroDetail.itemNum;
            }else if(this.showIdx ==0){
                this.dom_pro_hd.text = currenthp + '/'+ heroDetail.maxHp;
                this.dom_pro_damag.text = currentdamage+ '/'+ heroDetail.maxDamage; 
                this.dom_pro_effect.text = currenteffect+ '/'+ heroDetail.maxEffect; 
            }

            if(this.upgrade){
                Laya.Tween.from(this.attributeValBox, {y:335}, 600, null,null);
                this.upgrade = false;
            }

            // 确定按钮
           /* if(!heroDetail.had && this.btn_confirm.visible){
               this.btn_confirm.disabled = true;
            }else{
                this.btn_confirm.disabled = false;
            }*/

            // 升级按钮
         /*   if(!heroDetail.had && this.btn_upgrade.visible){
               this.btn_upgrade.disabled = true;
            }else{
                this.btn_upgrade.disabled = false;
            }*/
          
        }

        initEvent(){
            // 普攻描述
            this.btn_damage.on(EVENT_CLICK, this, ()=>{

                Sail.director.popScene(new Alert.Tips('普攻描述'));

            })

            // 技能描述
            this.btn_effect.on(EVENT_CLICK, this, ()=>{

                Sail.io.emit(GAME_CMDS.SKILLDETAIL);

            })

            // 确定
            this.btn_confirm.on(EVENT_CLICK, this, ()=>{

                console.log("this.type this.mapId this.heroDetail.hero_no",this.type,this.mapId,this.heroDetail.hero_no);
                if(this.type){
                    let dataType = {
                        heroNo: this.heroDetail.hero_no,
                        mapId : this.mapId,
                        type :  this.type
                    }
                    Sail.io.emit(GAME_CMDS.MATCH, dataType);
                    this.goBackCallBack && this.goBackCallBack();
                }else{
                    let data = {
                        heroNo: this.heroDetail.hero_no
                    }
                    Sail.io.emit(GAME_CMDS.CHOOESEHERO, data);
                    this.goBackCallBack && this.goBackCallBack();
                }
                
            })

            // 升级
            this.btn_upgrade.on(EVENT_CLICK, this, ()=>{
                let clips = this.dom_debris.text.split("/");
                let hadClips = Number(clips[0]);
                let upClips  = Number(clips[1]);
                if(hadClips >= upClips && hadClips>0 && this.heroDetail.level <9){
                    let data = {
                        userId :  GM.user_id, 
                        heroNo:   this.heroDetail.hero_no, 
                        level:    this.heroDetail.level,
                        coin:     this.heroDetail.coin,
                        had_item: this.heroDetail.had_item,
                        item_num: this.heroDetail.item_num
                    }
                    console.log("up",data);
                    Sail.io.emit(GAME_CMDS.UPBYITEM, data);
                    this.upgrade = true;
                }else{
                    let desc = ''
                    if(this.heroDetail.level ==9) {
                        desc = '已经达到满级，暂时不能升级'
                    }else{
                        desc= '碎片数量不够，暂时不能升级';
                    }
                    Sail.director.popScene(new Alert.Tips(desc));
                }
        
            })
        }

        heroSkillDetail(data){
            let desc = '技能描述';
            Sail.director.popScene(new Alert.Tips(desc));
        }


        exit () {
            Sail.io.unregister(this.ACTIONS);
        }
        
        resize (width, height, rate) {
            if(rate < 1){
                this.scale(rate, rate);
            }else{
                this.scale(1, 1);
            }
        }
    }

    Sail.class(Roledetail, "Com.Role.Roledetail");
}